<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['lang']){
	$_SESSION['show_lang']=$_RQ['lang'];
}
//未登录下单
//$this->model('member')->checklogin();
$return['mid']=$_SESSION['uid'];
$return['show_setmobile']=cfc('core')->check_setmobile(1);
$return['show_setmember']=cfc('core')->check_setmember(1);
if($_RQ['op']=='main'){
	$shopset = $this->plugin_setting('shopset');
	//营销设置
	$saleset=$this->plugin_setting('saleset');
	$member=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$member2=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('avatar','nickname','credit1','credit2'));
	$member=array_merge($member,$member2);
	$goodsid = intval($_RQ['goodsid']);
	//来自商户的订单
	$merchid = intval($_RQ['merchid']);
	if($_RQ['storeid']){
		$_SESSION['store_id']=intval($_RQ['storeid']);
	}
	$store_id=$_SESSION['store_id'];
	$sendtype=intval($_RQ['sendtype']);
	//判断是不是点餐
	$_SESSION['is_dish']=intval($_RQ['is_dish']);
	//返回链接
	$return['forward_url']=base64_encode('jy_weishop/order.list//fromcreate=1');
	//下单页
	$item=array();
	//提交数据
	$return['post'] = array();
	if(!$merchid){
		$carrier_list = pdo_getall('jy_weishop_store',array('uniacid' =>$_SESSION['uniacid'],'status'=>1,'type'=>array(1,3)),'','','paixu desc,id desc');
		if($carrier_list){
			foreach($carrier_list as &$pv){
				$pv['pcd']=json_decode($pv['pcd'],true);
			}
			$return['carrier_list']=$carrier_list;
			$return['selectstore']=$carrier_list['0'];
		}
	}else{
		$return['merch'] = pdo_get('jy_merch_user',array('uniacid' =>$_SESSION['uniacid'],'id'=>$merchid));
	}
	if($goodsid){
		$_SESSION['goodsid']=$goodsid;
		//直接购买。
		$return['post']['goodsid'] = $goodsid;
		$goodsfile=array('id','no_de_sales','isdiyprice','diyprice','invoice','type','buylevels','buygroups','title','merchid','status','pcate','total','subtitle','image','video','price','totalcnf','weight','active_plugin','active_id','storeids','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit','isverify','diyfields','api_id','api_pluginname');
		$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid),$goodsfile);
		if(!$goods){
			exi('指定商品不存在！','error','goback');
		}
		if($_SESSION['is_tomedia']){
			$goods['image']=$goods['image']?tomedia($goods['image']):'';
		}
		$diyfields=$this->model('goods')->get_diyformfields($goodsid);
		//配送设置
		//配送设置
		$goodsdispatch=$this->model('goods')->get_dispatch($goodsid);
		$goods=array_merge($goods,$goodsdispatch);
		//如果来自其他商户分销（修改商户ID）
		$frommerchid=$this->session('frommerchid');
		$goods['merchid']=$frommerchid?$frommerchid:$goods['merchid'];
		$this->model('goods')->check_buy($goods['id'],intval($_RQ['num']),intval($_RQ['optionsid']));
		//多商户商品：
		if($goods['merchid'] && !$merchid){
			$merchid=$goods['merchid'];
			$return['merch']=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goods['merchid']));
		}
		//购买权限：
		$levelinfo = $this->model('member')->get_level();
		if($goods['buylevels']){
			$fastvalue=substr($goods['buylevels'],0,1);
			if($fastvalue!=';'){
				$goods['buylevels']=';'.$goods['buylevels'];
			}
			$lastvalue=substr($goods['buylevels'],-1,1);
			if($lastvalue!=';'){
				$goods['buylevels']=$goods['buylevels'].';';
			}
			if(strpos($goods['buylevels'],';'.$levelinfo['id'].';')===false){
				exi('您没有权限购买该商品！','error','index/');
			}
		}
		$groupinfo = $this->model('member')->get_group();
		if($goods['buygroups']){
			$fastvalue=substr($goods['buygroups'],0,1);
			if($fastvalue!=';'){
				$goods['buygroups']=';'.$goods['buygroups'];
			}
			$lastvalue=substr($goods['buygroups'],-1,1);
			if($lastvalue!=';'){
				$goods['buygroups']=$goods['buygroups'].';';
			}
			if(strpos($goods['buygroups'],';'.$groupinfo['id'].';')===false){
				exi('您没有权限购买该商品！','error','index/');
			}
		}
		$goods['goodsid']=$goods['id'];
		$optionsid = intval($_RQ['optionsid']);
		$goods['storeid']=$store_id;
		$goods['optionid']=$optionsid;
		$goods['roomid']=intval($_RQ['roomid']);
		$goods=$this->model('activity')->get_upgoods($goods);
		//重置下单数量
		$goods['goodstotal']=$goods['total'];
		$goods['total']=intval($_RQ['num']);
		if($optionsid){
			$return['post']['optionid'] = $optionsid;
			if(!$goods['options']){
					exi('指定规格的商品不存在！','error','goback');
			}
			$specs = explode("_", $goods['options']['specs']);
			$specsall = pdo_getall('jy_weishop_goods_spec_item',array('id'=>$specs,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
			if($specsall){
					$optiontitle='';
					foreach($specsall as $k=>&$sp){
						if($k==0){
							$optiontitle .=$sp['title'];
						}else{
							$optiontitle .='，'.$sp['title'];
						}
					}
			}
			$goods['optiondata']= $optiontitle;
		}else{
			$goods['optionid']=0;
			$return['post']['optionid'] = 0;
		}
		if($goods['active_plugin'] && !$optionsid){
				//商品在做活动，检查下库存是否不足
				$this->model('activity')->check_stock($goods['id'],$goods['active_plugin'],$goods['active_id'],$goods['total']);
		}
		$return['post']['total'] = intval($_RQ['num']);
		$return['post']['invoice'] = $goods['invoice'];
		if($goods['type']==1 || $goods['type']==2){
			//虚拟商品
			$order['sendtype']=3;
		}elseif($goods['type']==3){
			//计时计次服务，发货类型必须是上门核销
			$order['sendtype']=2;
		}elseif($goods['type']==0 || $goods['isverify']){
			//实体商品，查找是否有门店，如果有门店则表示支持自提，否则只能是快递配送
			if(!$carrier_list && !$merchid){
				$order['sendtype']=1;
			}else{
				$return['sendtypes']=array(
					'1'=>'快递配送',
					'2'=>'自提',
				);
			}
		}
		$order=array(
			'sendtype'=>$order['sendtype'],
			'type'=>'one',
			'list'=>array($goods),
		);
	}else{
		//从购物车购买
		$order['sendtype']=1;//类型只能是快递配送
		if($_RQ['isbuyto']){
			$cart_list=$_SESSION['cart_list'];
			$_SESSION['cart_list']='';
			if(!$cart_list){
				exi('您没有选中商品！','error','jy_weishop/member.cart/');
			}
		}else{
			$cart_list=pdo_getall('jy_weishop_member_cart',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'selected'=>1),array('id','goodsid','total','optionid','merchid'));
			if(!$cart_list){
				exi('您的购物车没有选中商品！','error','jy_weishop/member.cart/');
			}
		}
		$diyfields=array();
		foreach($cart_list as $k=>&$cart){
			$goodsfile=array('id','no_de_sales','isdiyprice','diyprice','invoice','type','buylevels','buygroups','title','merchid','status','pcate','total','subtitle','image','video','price','totalcnf','weight','active_plugin','active_id','storeids','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit','isverify','diyfields','api_id','api_pluginname');
			$goods=pdo_get('jy_weishop_goods',array('id'=>$cart['goodsid']),$goodsfile);
			if(!$goods){
				exi('指定商品不存在！','error','goback');
			}
			if($merchid && $goods['merchid']!=$merchid){
				unset($cart_list[$k]);
				continue;			
			}
			if($_SESSION['is_tomedia']){
			$goods['image']=$goods['image']?tomedia($goods['image']):'';
			}
			$goods['diyfields']=$this->model('goods')->get_diyformfields($cart['goodsid']);
			if($goods['diyfields']){
				$diyfields=$goods['diyfields'];
			}
			//配送设置
			$goodsdispatch=$this->model('goods')->get_dispatch($cart['goodsid']);
			$goods=array_merge($goods,$goodsdispatch);
			$this->model('goods')->check_buy($cart['goodsid'],$cart['total'],$cart['optionid']);
			$goods['storeid']=$store_id;
			$goods['optionid']=$cart['optionid'];
			$goods=$this->model('activity')->get_upgoods($goods);
			//重置下单数量
			$goods['goodstotal']=$goods['total'];
			if($cart['optionid']){
				if(!$goods['options']){
					exi('指定规格的商品不存在！','error','goback');
				}
				$goods['price']=$goods['options']['price'];
				$specs = explode("_", $goods['options']['specs']);
				$specsall = pdo_getall('jy_weishop_goods_spec_item',array('id'=>$specs,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				if($specsall){
					$optiontitle='';
					foreach($specsall as $k=>&$sp){
						if($k==0){
							$optiontitle .=$sp['title'];
						}else{
							$optiontitle .='，'.$sp['title'];
						}
					}
				}
				$goods['optiondata']= $optiontitle;
			}
			//来自商户的分销
			$cart['merchid']=$cart['merchid']?$cart['merchid']:$goods['merchid'];
			if($goods['type']==1 || $goods['type']==2){
				//虚拟商品
				$order['sendtype']=3;
			}elseif($goods['type']==3){
				//计时计次服务，发货类型必须是上门核销
				$order['sendtype']=2;
			}elseif($goods['type']==0 || $goods['isverify']){
				//实体商品，查找是否有门店，如果有门店则表示支持自提，否则只能是快递配送
				if(!$carrier_list && !$merchid){
					$order['sendtype']=1;
				}else{
					$return['sendtypes']=array(
						'1'=>'快递配送',
						'2'=>'自提',
					);
				}
			}
			$cart=array_merge($goods,$cart);
			
		}
		$order=array(
			'sendtype'=>$order['sendtype'],
			'type'=>'cart',
			'list'=>$cart_list,
		);
	}
	$return['citypeisong']=$this->plugin_setting('citypeisong');
	if($return['citypeisong']['isopen']){
		$order['ismake']=$return['citypeisong']['ismake'];
	}
	//点餐机制：
	$order['sendtype']=$sendtype?:$order['sendtype'];
	if($_SESSION['table_id']){
		$order['sendtype']=2;
	}
	if($merchid){
		$order['merchid']=$merchid;
		$order['ismake']=$return['merch']['ismake'];
	}
	if(empty($order['list'])){
		exi('请先选择商品再下单！','error','jy_weishop/member.cart/');
	}
	//统计订单商品总数和总价
	$total=0;
	$total_price=0.00;
	$total_weight=0;
	$buyxiu_de_price=0;
	foreach($order['list'] as &$ov){
		if($ov['merchid']){
			$isid=pdo_getcolumn('jy_merch_user',array('id'=>$ov['merchid'],'uniacid'=>$_SESSION['uniacid']),'id');
			if(!$isid){
				$order['exitnomerch']=1;
			}
		}else{
			$order['exitnomerch']=1;
		}
		$total+=$ov['total'];
		$total_price+=$ov['total']*$ov['price'];
		$total_weight+=$ov['weight']*$ov['total'];
		$buyxiu_de_price+=$ov['buyxiu_de_price'];
	}
	$order['total']=$total;
	$order['total_price']=$total_price;
	$order['total_weight']=round(($total_weight/1000)*1000,2);
	$order['buyxiu_de_price']=$buyxiu_de_price;
	//计算运费
	$item['address']=array();
	$order['dispatchprice']=0;
	$order['sendtype']=$order['sendtype']?:1;
	//刷新运费
	if(!$order['sendtype'] || $order['sendtype']==1 || $return['sendtypes']){
		$up_dispatch=$this->model('order/dispatch')->up_dispatch($order,$item,$_RQ['addressid'],$_SESSION['store_id'],$return['merch']);
		$order=$up_dispatch['order']?:$order;
		$item=$up_dispatch['item']?:$item;
	}
	if($order['is_peisong']){
		$return['sendtypes']=array(
				'1'=>'同城配送',
				'2'=>'到店自取',
		);
	}
	if($_SESSION['is_dish']){
		$return['sendtypes']=array(
			'1'=>'外卖配送',
			'2'=>'到店自取',
		);
	}
	//会员折扣价
	$order['level_de_price']=$this->model('activity')->get_level_de_price($order);
	//运势折扣价
	$order['luck_de_price']=$this->model('activity')->get_luck_de_price($order);
	//满减优惠价格：
	$sale_enough=$saleset['enough'];
	if($sale_enough){
		foreach($sale_enough as $k=>$ev){
			if($ev['man']>$order['total_price']){
				unset($sale_enough[$k]);
			}
		}
		paixu($sale_enough,'man','desc');
	}
	$order['sale_enough']=$sale_enough;
	$order['enough_de_price']=$sale_enough[0]['jian']?$sale_enough[0]['jian']:0.00;
	//送积分
	$order['credit']=$this->model('activity')->get_buy_credit_order($order);
	//积分抵扣
	$deduct_res=$this->model('activity')->get_deduct_credit_order($order,$saleset,$member);
	$order['deduct_credit']=$deduct_res['deduct_credit'];
	$order['delcredit1']=$deduct_res['delcredit1'];
	//优惠券优化价格：
	$order['coupon_de_price']=0.00;
	if(!$order['no_coupon_discount']){
		//整理优惠券数据，先获取用户所有待使用优惠券，再从中筛选出未过期的优惠券且本次下单可用的优惠券！
		$newcoupons=$this->model('coupon')->getmycoupon_ing(1,$order['total_price'],$order['level_de_price'],$order['list']);
		$order['coupon_de_price']=$newcoupons[0]['de_price'];
		$item['couponid']=$newcoupons[0]['id'];
		$return['couponcount']=count($newcoupons);
	}
	$return['coupons']=$newcoupons?$newcoupons:array();
	//整理收货地址信息
	if($store_id){
		$return['selectstore']=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$store_id));
	}
	if($return['selectstore']['id']){
		$return['selectstore']['juli']=1;
	}
	$return['address_list']=pdo_getall('core_address',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$return['address_item']=array();
	//提交数据
	$return['post']['address'] = $item['address']?:array('city'=>'');
	$return['post']['merchid'] = $merchid;
	$return['post']['couponid'] = $newcoupons[0]['id'];
	$return['post']['make_type'] = 0;
	$return['post']['dayid'] = 0;
	$return['post']['title5'] = $newcoupons[0]['coupon']['title5'];
	$return['post']['deduct'] = $newcoupons[0]['coupon']['deduct'];
	$return['post']['discount'] = $newcoupons[0]['coupon']['discount'];
	$return['post']['backtype'] = $newcoupons[0]['coupon']['backtype'];
	$return['post']['addressid'] = $item['addressid'];
	$return['post']['sendtype'] = $order['sendtype']?:1;
	$return['post']['sendtime'] = strtotime("+2 day");
	$return['post']['storeid'] = $return['selectstore']['id'];
	$return['post']['deliverytime'] = date("m月d日",strtotime("+2 day"));
	$return['post']['diyfields'] = $diyfields;
	$return['post']['dispatchprice']=$order['dispatchprice'];
	//配送
	$order['deliverytime'] = $return['post']['deliverytime'];
	//打包费
	$order['out_upfee']=$return['merch']['out_upfee']>0?$return['merch']['out_upfee']:0;
	//整理最终价(暂不扣除优惠券)post是最终价，order是未减优惠券的价
	$order['endprice']=round($order['total_price'] + $order['dispatchprice'] + $order['out_upfee'] - $order['level_de_price'] - $order['luck_de_price']-$order['buyxiu_de_price']  - $order['enough_de_price'] - $order['deduct_credit'],2);
	$order['no_dispatch_price']=round($order['total_price'] + $order['out_upfee'] - $order['level_de_price'] - $order['luck_de_price']-$order['buyxiu_de_price'] - $order['enough_de_price'] - $order['deduct_credit'],2);
	$order['endprice']=$order['endprice']>=0?$order['endprice']:0;
	$return['post']['endprice']=$order['endprice']- $order['coupon_de_price'];
	$return['post']['endprice']=$return['post']['endprice']>=0?$return['post']['endprice']:0;
	$return['post']['no_dispatch_price']=$order['no_dispatch_price']- $order['coupon_de_price'];
	$return['post']['no_dispatch_price']=$return['post']['no_dispatch_price']>=0?$return['post']['no_dispatch_price']:0;
	/*
		赠品
		先根据订单金额找赠品，再根据商品找赠品
	*/
	$goods_gifts=$this->pdo_getall('goods_gift',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'activity'=>0));
	//$current_gift=array();
	if($goods_gifts){
		foreach($goods_gifts as $gift){
			$gift['onlinetime']=json_decode($gift['onlinetime'],true);
			if($gift['onlinetime']['tstart']<time() && $gift['onlinetime']['tend']>time() && $gift['orderprice'] && $gift['orderprice']<$order['no_dispatch_price']){
				if(!$current_gift || $current_gift['orderprice']<$gift['orderprice']){
					$current_gift=$gift;
				}
			}
		}
	}
	if(!$current_gift){
		$goods_gifts=$this->pdo_getall('goods_gift',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'activity'=>1));
	}
	if($goods_gifts){
		foreach($goods_gifts as $gift){
			$gift['onlinetime']=json_decode($gift['onlinetime'],true);
			if($gift['onlinetime']['tstart']<time() && $gift['onlinetime']['tend']>time() && $gift['goodsid']){
				foreach($order['list'] as $goods){
					if($goods['goodsid']==$gift['goodsid']){
						$current_gift=$gift;
						break;
					}
				}
			}
			if($current_gift){
				break;
			}
		}
	}
	if($current_gift){
		$order['gift_id']=$current_gift['id'];
		$return['gifts']=json_decode($current_gift['gifts'],true);
		foreach($return['gifts'] as &$gv){
			$gv['price']=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['id'],'uniacid'=>$_SESSION['uniacid']),'price');
			if($_SESSION['is_tomedia']){
				$gv['image']=$gv['image']?tomedia($gv['image']):'';
			}
		}
	}
	$order['total_price']=round($order['total_price'],2);
	$_SESSION['order']=$return['order']=$order;
	$item['sendtype']=1;
	$return['item']=$item;
	//获取行政数据
	$return['province']=cfc('tools')->pcd();
	//自提点
	
	set_html('title','确认订单');
	//代付
	$peerpay_setting=$this->plugin_setting('peerpay');
	if($peerpay_setting && $peerpay_setting['open'] && $order['endprice']>0){
		$return['peerpay']=true;
	}
	//获取小程序订阅消息ID
	$tmplIds=array();
	if($_SESSION['wxapp_id']){
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('3534',[1,2,3,4],'pay2buyer','订单支付成功通知','jy_weishop');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('3534',[1,2,3,4],'send2buyer','订单发货通知','jy_weishop');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('3534',[1,2,3,4],'receive2buyer','订单确认收货通知','jy_weishop');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}	
	}
	$return['tmplIds']=$tmplIds;
	if(intval($return['setting']['index_tpl']) && $_SESSION['os'] != 'windows'){
		$return['htmls']=$this->template_url('order/create_a'.$return['setting']['index_tpl']);
	}
	$return['openid']=$_SESSION['openid'];
	exi($return);
}

if($_RQ['op']=='juli'){
	//判断距离
		$post=get_postdata();
		$merchid=pdo_getcolumn('jy_weishop_goods',array('id'=>$_SESSION['goodsid']),'merchid');
		$l=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),array('lat','lng'));
		if($l['lng'] && $l['lat']){
			$d=cfc('tools')->getdistance($post['lat_lng']['lng'],$post['lat_lng']['lat'],$l['lng'],$l['lat']);
			if($d/1000>$post['juli']){
				exi('超出配送距离,无法购买','error','/goods.detail//id='.$_SESSION['goodsid']);
				exi('success');
			}else{
				exi('success');
			}
		}	
}
// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	//整理订单数据
	$order=$_SESSION['order'];
	//公共参数
	if($post['out_time']){
		//配送时间
		$days = array(
				0=>date("Y-m-d",time("day")),
				1=>date("Y-m-d",strtotime("+1 day")),
				2=>date("Y-m-d",strtotime("+2 day")), 
				3=>date("Y-m-d",strtotime("+3 day")), 
				4=>date("Y-m-d",strtotime("+4 day")),
				5=>date("Y-m-d",strtotime("+5 day")),
				6=>date("Y-m-d",strtotime("+6 day")),
		);
		$dayid=intval($post['dayid']);
		$post['out_time']=$days[$dayid].' '.$post['out_time'];
	}
	$new_order=array(
		'mid'=>$_SESSION['uid'],
		'goodsprice'=>$order['total_price'],
		'remark'=>trim($post['remark']),
		'goods'=>$order['list'],
		'merchid'=>$order['merchid'],
		'sendtype'=>intval($post['sendtype'])?:1,
		'diyfields'=>ijson_encode($post['diyfields']),
		'out_time'=>trim($post['out_time']),
		'make_type'=>intval($post['make_type']),
		'is_peisong'=>$order['is_peisong'],
		'out_upfee'=>$order['out_upfee']?:0,
		'mobile'=>$post['address']['mobile'],
		'address'=>$post['address'],
	);
	$new_order['invoce']=array(
		'type'=>$post['invoicetype'],
		'goodstype'=>$post['invoicegoodstype'],
		'name'=>$post['invoicename'],
		'code'=>$post['invoicecode'],
		'email'=>$post['email'],
	);
	//会员折扣
	if(!$order['no_member_discount'] && $order['level_de_price']){
		$new_order['level_de_price']=$order['level_de_price'];
	}
	//运势折扣
	if($order['luck_de_price']){
		$new_order['luck_de_price']=$order['luck_de_price'];
	}
	//买家秀
	if($order['buyxiu_de_price']){
		$new_order['buyxiu_de_price']=$order['buyxiu_de_price'];
	}
	//满减
	$new_order['enough_de_price']=$order['enough_de_price'];
	//抵扣
	$new_order['deduct_credit']=$order['deduct_credit']?:0;
	$new_order['price']=$new_order['goodsprice']+$new_order['out_upfee']-intval($new_order['enough_de_price'])-$new_order['level_de_price']-$new_order['luck_de_price']-$new_order['buyxiu_de_price']-$new_order['deduct_credit'];
	//优惠券
	if(!$order['no_coupon_discount']){
		$couponid=intval($post['couponid']);
		//获取优惠信息
		$getcouponfee=$this->model('coupon')->getorderfee($couponid,$new_order['price']);
		if($getcouponfee){
			$new_order=array_merge($new_order,$getcouponfee);
		}
	}
	//自动分配门店
	$new_order['storeid']=intval($post['storeid']);
	if($new_order['sendtype']==1){
		//快递配送
		$new_order['addressid']=intval($post['addressid']);
		if(!$new_order['addressid'] && $_SESSION['uid']){
			exi('请先选择收货地址！','error');
		}
		$pcd=pdo_getcolumn('core_address',array('id'=>$new_order['addressid']),'pcd');
		if($pcd){
			$pcd=json_decode($pcd,true);
			$new_order['dispatchprice']=$post['dispatchprice'];
			$new_order['price']+=$new_order['dispatchprice'];
		}		
		if($post['juli']){
			$merchid=pdo_getcolumn('jy_weishop_goods',array('id'=>$_SESSION['goodsid']),'merchid');
			$l=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),array('lat','lng'));
			if($l['lng'] && $l['lat']){
				$d=cfc('tools')->getdistance($post['lat_lng']['lng'],$post['lat_lng']['lat'],$l['lng'],$l['lat']);
				if($d/1000>$post['juli']){
					exi('超出配送距离,无法购买','error','/goods.detail//id='.$_SESSION['goodsid']);
				}
			}
		}		
	}elseif($new_order['sendtype']==2){
		//门店自提
		if(!$new_order['storeid'] && !$order['merchid']){
			exi('请先选择门店！','error');
		}
	}elseif($new_order['sendtype']==3){
		//虚拟发货
		
	}
	$new_order['price']=$new_order['price']>0?$new_order['price']:0;
	if($new_order['price']<0){
		exi('订单金额不能小于零！','error');
	}
	if($order['gift_id']){
		$new_order['gift_id']=$order['gift_id'];
	}
	if($_SESSION['is_dish']){
		$new_order['isdish']=1;
	}
	//如果需要制作，配送状态为未制作！
	if($order['ismake']==1){
		$new_order['send_status']=11;
	}
	$order_id=$this->model('order')->create($new_order);
	unset($_SESSION['order']);
	if($_SESSION['current_plugin'] && $_SESSION['current_plugin']!='core' && $_SESSION['current_plugin']!='jy_weishop'){
		$success_url=plugin($_SESSION['current_plugin'])->weishop_order_detail;
		if($success_url){
			$success_url=$_SESSION['current_plugin'].$success_url.'/id='.$order_id;
			$error_url=$success_url;
		}
	}
	$params=array(
		'id'=>$order_id,
		'title'=>'购买商品',
		'fee'=>$new_order['price'],
		//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
		'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
		'back_url'=>'jy_weishop/order.create/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
		'success_url'=>$success_url?:'jy_weishop/order.list/',
		'error_url'=>$error_url?:'jy_weishop/order.list/',
	);
	$payid=cfc('pay')->create_pay_order($params);
	$this->pdo_update('order',array('pay_id'=>$payid),array('id'=>$order_id));
	if($post['peerpay']){
		$peerpay_setting=$this->plugin_setting('peerpay');
		if($peerpay_setting && $peerpay_setting['open']){
			exi('','','/order.peerpay//orderid='.$order_id);
		}
	}
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
		'success_url'=>$success_url,
		'error_url'=>$error_url,
	);
	exi($res);
}
if($_RQ['op']=='get_freight'){
	$merchid=intval($_RQ['merchid']);
	$addressid=intval($_RQ['addressid']);
	$addressid=$addressid?:intval($_RQ['address']);
	if(!$addressid){
		exi('请先选择地址！','error');
	}
	if($merchid){
		$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
	}
	$up_dispatch=$this->model('order/dispatch')->up_dispatch($_SESSION['order'],array(),$addressid,$_SESSION['store_id'],$merch);
	$freight=$up_dispatch['get_freight'];
	//多少钱起送
	$freight['no_out_price']=$freight['no_out_price']?:0;
	if($_RQ['showbaoyou']){
		$freight['baoyou_man']=$freight['baoyou_man']?:0;
		exi($freight);
	}else{
		exi($freight['fee']);
	}
	
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_weishop/order.list/');
	}
	$order=pdo_get('jy_weishop_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_weishop/order.list/');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_weishop/order.list/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	$this->model('order')->order_pay($order['id'],$params);
	$message='有新的订单支付成功！';
	if($order['merchid']){
		$mid=pdo_getcolumn('jy_merch_user',array('id'=>$order['merchid'],'uniacid'=>$_SESSION['uniacid']),'mid');
		//cfc('jinyun_socket')->send2uid($message,$mid);
	}else{
		//cfc('jinyun_socket')->send2uid($message,$_SESSION['uid']);
	}
	exi('提交成功!');
}

?>